﻿//============================================================
//提示框及对话框
//============================================================
function Dialog() {
    var _this = this;
    _this.ID = ""; //生成的对话框ID，不能为空
    _this.Title = ""; //对话框的标题
    _this.Type = ""; //message,confirm,dialog,window
    _this.ShowMask = true; //true,false 是否显示蒙板
    _this.MessageText = ""; //当类型为message和confirm时，输出的内容
    _this.Width = "260px"; //初始宽度,类型为dialog时不需要设置该属性
    _this.Height = "120px"; //初始高度,类型为dialog时不需要设置该属性
    _this.ShowClose = true; //是否显示关闭按钮
    _this.ShowMinBtn = false; //是否显示最小化按钮
    _this.ShowMaxBtn = false; //是否显示最大化按钮
    _this.ContentObj = new Object(); //指定的html对象
    _this.ContentAlign = "center"; //对话框中内容的横向位置
    _this.ContentValign = "middle"; //对话框中内容的纵向位置
    _this.ShowMax = false; //是否默认最大化显示
    _this.NormalPositionLeft = "";
    _this.NormalPositionTop = "";
    _this.Draggable = false; //是否可以拖动
    _this.Parameter = ""; //当类型为confirm时，点击确定按钮需要的参数
    _this.ConfirmFun = null; //当类型为confirm时，点击确定按钮执行的外部方法名
    _this.Url = ""; //当类型为window的时候，需要显示的Url
    _this.ClosedFun = null; //对话框关闭时执行的方法名称

    _this.initDialog = function () {
        if (_this.ID == "") {
            alert("未设置ID值！");
            return;
        }
        CreateDialog();
    }

    _this.hide = function () {
        var vDialogID = "dlg_" + _this.ID;
        $("#" + vDialogID).hide();

        if (_this.ShowMask)
            hideMask();
        if (_this.ClosedFun != null) {
            _this.ClosedFun();
        }
    }

    _this.show = function () {
        var vDialogID = "dlg_" + _this.ID;

        var vTable = $("#" + vDialogID);
        switch (_this.Type.toLowerCase()) {
            case "message":
                $(vTable).find(".msgContext").html(_this.MessageText);
                var vBtnClose = $(vTable).find("#" + vDialogID + "_msgClose");
                $(vBtnClose).focus();
                break;
            case "confirm":
                $(vTable).find(".msgContext").html(_this.MessageText);
                $(vTable).find("#" + vDialogID + "_msgOk").focus();
                if (_this.ConfirmFun) {
                    if (_this.Parameter) {
                        $(vTable).find(".btn_ok").unbind("click").click(function () {
                            _this.ConfirmFun(_this.Parameter);
                            _this.hide();
                        });
                    }
                }
                break;
            case "dialog":
                SetBodyHeight(_this.Width, _this.Height);
                break;
            case "window":
                if (_this.ShowMax) {
                    MaxDialog();
                }
                break;
        }
        $(_this.ContentObj).show();
        $(vTable).show();

        if (_this.ShowMask)
            showMask(99);
    }

    _this.updateTitle = function (title) {
        var vHeadID = "dlg_" + _this.ID + "_head";
        $("#"+vHeadID).find(".title").attr("title", title).text(title);
    }

    var CreateDialog = function () {
        var vDialogID = "dlg_" + _this.ID;
        if ($("#" + vDialogID).length == 0) {//如果该对话框没有被创建
            var vTable = $("<table border='0px' cellpadding='0px' cellspacing='0px'/>").addClass("dlg").attr("id", vDialogID).css("z-index", 100001);
            var vHeadID = "dlg_" + _this.ID + "_head";
            var vHead = $("<tr/>").attr("id", vHeadID).css("cursor", "move");
            var vBody = $("<tr/>");
            var vBottom = $("<tr/>");

            var vHead_l = $("<td/>").addClass("dlg_h_l");
            var vTitle = $("<span/>").addClass("title").attr("title", _this.Title).text(_this.Title);
            var vClose = $("<div/>").addClass("close");
            var vMin = $("<div/>").addClass("min");
            var vMax = $("<div/>").addClass("max");
            var vNor = $("<div/>").addClass("nor");
            var vHead_m = $("<td/>").addClass("dlg_h_m").append(vTitle).append(vClose).append(vMax).append(vNor).append(vMin);
            var vHead_r = $("<td/>").addClass("dlg_h_r");

            var vBody_l = $("<td/>").addClass("dlg_m_l").append($("<div/>").css("height", _this.Height));
            var vBody_m = $("<td/>").addClass("dlg_m_m").css("width", _this.Width).css("height", _this.Height).attr("align", _this.ContentAlign).attr("valign", _this.ContentValign);
            var vBody_r = $("<td/>").addClass("dlg_m_r").append($("<div/>").css("height", _this.Height));

            var vBottom_l = $("<td/>").addClass("dlg_b_l");
            var vBottom_m = $("<td/>").addClass("dlg_b_m");
            var vBottom_r = $("<td/>").addClass("dlg_b_r");

            //处理内容块的显示
            switch (_this.Type.toLowerCase()) {
                case "message":
                    MessageTable(vBody_m, "message");
                    break;
                case "confirm":
                    MessageTable(vBody_m, "confirm");
                    break;
                case "dialog":
                    $(vBody_m).append($(_this.ContentObj));
                    //判断自身和插入的对象大小哪一个更大，取最大值
                    _this.Width = $(_this.ContentObj).width() > $(vBody_m).width() ? $(_this.ContentObj).width() : $(vBody_m).width();
                    _this.Height = $(_this.ContentObj).height() > $(vBody_m).height() ? $(_this.ContentObj).height() : $(vBody_m).height();
                    break;
                case "window":
                    var frameid = vDialogID + "_frame";
                    var frame = $("<iframe height='99%' width='100%' frameborder='0'/>").attr("id", frameid).attr("name", frameid).attr("src", _this.Url);
                    $(vBody_m).append(frame);
                    $(frame).load(function () {
                        var frametitle = _this.Title + " - " + $(this).contents().find("head").children("title").text();
                        $(vTitle).text(frametitle);
                        var vBarID = "dlg_bar_" + _this.ID;
                        $("#" + vBarID).find(".title").text(frametitle);
                    });
                    CreateWindowBar(); //创建工具条
                    break;
            }

            $(vHead).append(vHead_l).append(vHead_m).append(vHead_r);
            $(vBody).append(vBody_l).append(vBody_m).append(vBody_r);
            $(vBottom).append(vBottom_l).append(vBottom_m).append(vBottom_r);

            $(vTable).append(vHead).append(vBody).append(vBottom);
            $("body").append(vTable);

            if (_this.Draggable) {
                $(vTable).draggable({ handle: "#" + vHeadID });
            }
            else {
                $(vHead).css("cursor", "default");
            }

            //处理相关部件显示
            if (_this.ShowClose == false) {
                $(vClose).hide();
            }

            if (_this.ShowMinBtn == false) {
                $(vMin).hide();
            }
            else {//最小化时的处理
                $(vMin).click(function () {
                    MinDialog();
                });
            }

            if (_this.ShowMaxBtn == false) {
                $(vMax).hide();
                $(vNor).hide();
            }
            else {
                $(vHead_m).dblclick(function () {
                    if ($(vMax).css("display") == "block") {
                        MaxDialog();
                        $(vMax).hide();
                        $(vNor).show();
                    }
                    else {
                        NormalDialog();
                        $(vMax).show();
                        $(vNor).hide();
                    }
                });

                $(vMax).click(function () {//最大化按钮单击事件
                    MaxDialog();
                    $(vMax).hide();
                    $(vNor).show();
                });

                $(vNor).click(function () {//还原按钮单击事件
                    NormalDialog();
                    $(vMax).show();
                    $(vNor).hide();
                });

                if (_this.ShowMax == false) {
                    $(vMax).show();
                    $(vNor).hide();
                }
                else {
                    MaxDialog();
                }
            }

            //处理相关事件
            $(vClose).click(function () {//关闭按钮单击事件
                _this.hide();
            });

            $(vTable).hide();

            //var vMarginTop = ($(window).height() - _this.Height) / 2;
            var vMarginTop = ($(window).height() - $(vTable).height()) / 2;
            newPos = new Object();
            newPos.left = ($(window).width() - $(vTable).width()) / 2;
            newPos.top = vMarginTop;

            if (vMarginTop > 0) {

            }
            else {
                newPos.top = 150;
            }

            $(vTable).offset(newPos);
        }
    }

    var SetBodyHeight = function (vWidth, vHeight) {
        var vDialogID = "dlg_" + _this.ID;
        var vTable = $("#" + vDialogID);
        var vBody_l = $(vTable).find(".dlg_m_l");
        var vBody_m = $(vTable).find(".dlg_m_m");
        var vBody_r = $(vTable).find(".dlg_m_r");

        $(vBody_m).width(vWidth).height(vHeight);
        $(vBody_l).children("div").height(vHeight);
        $(vBody_r).children("div").height(vHeight);
    }

    var MaxDialog = function () {//对话框最大化
        var vDialogID = "dlg_" + _this.ID;
        var vTable = $("#" + vDialogID);

        var vTd = $(vTable).find(".dlg_h_m");
        vMax = $(vTd).children(".max");
        vNor = $(vTd).children(".nor");
        $(vMax).hide();
        $(vNor).show();

        var vWidth = document.body.clientWidth - 32;
        var vHeight = $("html").height() - 56;
        if (vHeight < 100)
            vHeight = $(document).height() - 56; // window.screen.height - 140;

        _this.NormalPositionLeft = $(vTable).offset().left;
        _this.NormalPositionTop = $(vTable).offset().top;

        $(vTable).attr("style", "position:absolute;left:0px;top:0px;z-index:100");
        SetBodyHeight(vWidth, vHeight);
    }

    var NormalDialog = function () {//还原初始大小
        var vDialogID = "dlg_" + _this.ID;
        var vTable = $("#" + vDialogID);

        newPos = new Object();
        newPos.left = _this.NormalPositionLeft;
        newPos.top = _this.NormalPositionTop;

        $(vTable).offset(newPos);
        SetBodyHeight(_this.Width, _this.Height);
    }

    var MinDialog = function () {//对话框最小化
        var vDialogID = "dlg_" + _this.ID;
        var vTable = $("#" + vDialogID);
        _this.hide();
        _this.showBar();       
    }

    var MessageTable = function (vBody_m, Type) {//处理message和confirm的显示内容
        var vDialogID = "dlg_" + _this.ID;
        var vMsgTable = $("<table border='0px' cellpadding='0px' cellspacing='0px'/>").css("width", _this.Width).css("height", _this.Height);
        var vMsgText = $("<tr/>").append($("<td class='msgContext'/>").append(_this.MessageText)).css("text-align", "center").height($(vMsgTable).height() - 45);
        var vMsgContent = $("<td align='right'/>").css("height", "45px;")       
        var vMsgBtnClose = $("<div class='btn_normal' style='width:60px'>关 闭</div>").attr("id", vDialogID + "_msgClose"); //.css("margin-right", "10px");
        if (Type == "confirm") {            
            var vMsgBtnOk = $("<div class='btn_normal btn_ok' style='width:60px'>确 定</div>").attr("id", vDialogID + "_msgOk");//.css("margin-left", "10px").css("margin-right", "10px");
            $(vMsgContent).append(vMsgBtnOk).attr("align", "center");

            $(vMsgBtnOk).unbind("click").click(function () {
                if (_this.ConfirmFun) {
                    if (_this.Parameter)
                        _this.ConfirmFun(_this.Parameter);
                    else
                        _this.ConfirmFun();
                }
                _this.hide();
            });
        }
        $(vMsgContent).append(vMsgBtnClose);
        var vMsgBottom = $("<tr/>").append(vMsgContent);
        $(vMsgTable).append(vMsgText).append(vMsgBottom);
        $(vBody_m).append(vMsgTable);
        $(vMsgBtnClose).click(function () {
            _this.hide();
        });
    }

    var CreateWindowBar = function () {//类型为window时增加一个bar，用于缩小后的还原
        var vBarID = "dlg_bar_" + _this.ID;
        var vBar = $("<div/>").attr("id", vBarID).addClass("dlg_bar").css("z-index", 998); ;
        var vBar_l = $("<div/>").addClass("dlg_bar_l");
        var vBar_m = $("<div/>").addClass("dlg_bar_m");
        var vBar_r = $("<div/>").addClass("dlg_bar_r");
        var vTitle = $("<span/>").addClass("title").attr("title", _this.Title).text(_this.Title);
        var vClose = $("<div/>").addClass("close");
        var vMax = $("<div/>").addClass("max");

        $(vBar_m).append(vTitle).append(vClose).append(vMax);
        $(vBar).append(vBar_l).append(vBar_m).append(vBar_r);
        $("body").append(vBar);

        $(vClose).click(function () {
            var vBarID = "dlg_bar_" + _this.ID;
            $("#" + vBarID).hide();
        });

        $(vMax).click(function () {
            _this.show();
            _this.hideBar();
        });

        $(vBar).draggable({ handle: ".dlg_bar_m" });
    }

    _this.showBar = function () {//显示bar
        var vBarID = "dlg_bar_" + _this.ID;
        $("#" + vBarID).show();
    }

    _this.hideBar = function () {//隐藏bar
        var vBarID = "dlg_bar_" + _this.ID;
        $("#" + vBarID).hide();
    }

    _this.changeURL = function (Url) {//改变要显示的网页地址
        var vDialogID = "dlg_" + _this.ID;
        var vTitle = $("#" + vDialogID).find(".title").text(_this.Title);
        $("#" + vDialogID + "_frame").attr("src", Url);
    }
}
